from sys import path as path2
from time import sleep, localtime, strftime
import base64
from threading import Thread, active_count

import requests
from bs4 import BeautifulSoup, Comment
from rich import print as rprint

path2.append('c:/users/tian/desktop')
from Service import service, ua
from service import title2tag, req

class Wuming1:
    def __init__(self, url):
        service.path = 'd:/data/wuming.db'
        self.URL = url
        self.newNum = 0     # 新增记录的数量

    def prepare(self, startIndex, endIndex):
        for i in range(startIndex, endIndex):
            indexUrl = f'{self.URL}/list.php?class=guochan&page={i}'
            while True:
                try:
                    node_a = req.req2(indexUrl).find('ul', class_='list')('a')[1:-2]
                    break
                except Exception as e:
                    rprint('[red]索引页解析出错')
                    rprint('报错信息：', e)
                    sleep(3)
            for a in node_a:
                Thread(target=self.get, args=(a, i)).start()
                sleep(0.08)
                while True:
                    if active_count() < 21:
                        break
            sleep(0.1)
        while True:         # 等待爬取线程都结束
            if active_count() == 1:
                break
            else:
                sleep(0.2)
        rprint(f'Wuming1 爬取完毕，本次新增 {self.newNum} 条数据')

    def get(self, a, i):
        _btitle = base64.b64decode(a.script.string.split("'")[1])     # 二进制串
        title = ''.join(_btitle.decode('utf-8').split()).lower()      # 删除空格或多个连续空格
        tag = title2tag.aboutTitle(service.path, title, i, 3)
        if not tag:
            return
        url = self.URL + a.attrs['href']
        _div = req.req2(url).find('div', id='content')
        postDate = _div.p.find(string=lambda text: isinstance(text, Comment))
        size = _div('p')[2].get_text()
        duration = _div('p')[3].get_text()
        link = self.URL + _div.find('div', class_='download').a.attrs['href']
        magnet = req.req2(link).p.string.split('：')[-1].lower()
        recordDate = strftime('%Y-%m-%d %H:%M', localtime())
        status = 0
        service.exec('insert into guochan (tag, title, url, link, magnet, size, duration, postdate, recorddate, status) values (?, ? ,? , ?, ?, ?, ?, ?, ?, ?)',
                    (tag, title, url, link, magnet, size, duration, postDate, recordDate, status))
        self.newNum += 1
        rprint(f'{self.newNum} ({i})')


    

